/*
 * Copyright 2011-2021 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.jmh.benchmark;

import java.util.stream.IntStream;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

/**
 * Sum Benchmark.
 *
 * @author liym
 * @since 2020-05-24 20:04 新建
 */
public class SumBenchmark {

    private static final int COUNT = 1000;

    public SumBenchmark() {}

    @Benchmark
    public int forSum() {
        int sum = 0;
        for (int i = 0; i < COUNT; i++) {
            sum += i;
        }
        return sum;
    }

    @Benchmark
    public int streamSum() {
        return IntStream.range(0, COUNT).sum();
    }

    public static void main(String[] args) throws RunnerException {
        Options options = new OptionsBuilder()
                .include(SumBenchmark.class.getName())
                // .include(SumBenchmark.class.getName() + ".*streamSum*")
                .warmupIterations(5)
                .measurementIterations(5)
                .forks(1)
                .build();

        new Runner(options).run();
    }

}
